using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

namespace Paradoxlost.WebParts
{
	[Guid("c9bd8631-c2d3-4bd8-b57e-3491af0b4b78")]
	public partial class ExchangeCalendarWebPart : WebPart
	{
		private bool _error = false;

		private HtmlGenericControl calendarDiv;

		[Personalizable(PersonalizationScope.Shared)]
		[WebBrowsable(true)]
		[System.ComponentModel.Category("Exchange Server")]
		[WebDisplayName("Server Address")]
		[WebDescription("Address of the EWS Service")]
		public string EwsAddress { get; set; }

		[Personalizable(PersonalizationScope.Shared)]
		[WebBrowsable(true)]
		[System.ComponentModel.Category("Exchange Server")]
		[WebDisplayName("Server Username")]
		[WebDescription("Username for the EWS Service")]
		public string EwsUsername { get; set; }

		[Personalizable(PersonalizationScope.Shared)]
		[WebBrowsable(true)]
		[System.ComponentModel.Category("Exchange Server")]
		[WebDisplayName("Server Password")]
		[WebDescription("Password for the EWS Service")]
		public string EwsPassword { get; set; }

		[Personalizable(PersonalizationScope.Shared)]
		[WebBrowsable(true)]
		[System.ComponentModel.Category("Exchange Server")]
		[WebDisplayName("Mailbox")]
		[WebDescription("Exchange mailbox to query")]
		public string EwsMailBox { get; set; }

		public ExchangeCalendarWebPart()
		{
			this.ExportMode = WebPartExportMode.All;
		}

		/// <summary>
		/// Create all your controls here for rendering.
		/// Try to avoid using the RenderWebPart() method.
		/// </summary>
		protected override void CreateChildControls()
		{
			if (!_error)
			{
				try
				{
					base.CreateChildControls();

					this.calendarDiv = new HtmlGenericControl("div");
					this.calendarDiv.ID = "calendar";

					Controls.Add(this.calendarDiv);

					HtmlLink link = new HtmlLink();
					link.Attributes.Add("rel", "stylesheet");
					link.Attributes.Add("type", "text/css");
					link.Href = Page.ClientScript.GetWebResourceUrl(this.GetType(), "Paradoxlost.WebParts.Resources.fullcalendar.css"); ;

					Page.Header.Controls.Add(link);
				}
				catch (Exception ex)
				{
					HandleException(ex);
				}
			}
		}

		/// <summary>
		/// Ensures that the CreateChildControls() is called before events.
		/// Use CreateChildControls() to create your controls.
		/// </summary>
		/// <param name="e"></param>
		protected override void OnLoad(EventArgs e)
		{
			if (!_error)
			{
				try
				{
					base.OnLoad(e);
					this.EnsureChildControls();

					// Your code here...
					if (!string.IsNullOrEmpty(EwsAddress) && !string.IsNullOrEmpty(EwsUsername))
					{
						RegisterScripts();
					}
					else
					{
						this.calendarDiv.InnerText = "Calendar not configured";
					}
				}
				catch (Exception ex)
				{
					HandleException(ex);
				}
			}
		}

		/// <summary>
		/// Clear all child controls and add an error message for display.
		/// </summary>
		/// <param name="ex"></param>
		private void HandleException(Exception ex)
		{
			this._error = true;
			this.Controls.Clear();
			this.Controls.Add(new LiteralControl(ex.Message));
		}
	}
}
